import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/state_manager.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

import '../../utils/persistent_storage.dart';

class PeopleMainLogic extends GetxController {
  ScrollController scrollController = ScrollController();
  double opacity = 0.0;

  RxString userName = '江南第一深情'.obs;

  RxString headImg =
      'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/people/dog.jpg'
          .obs;

  PersistentStorage ps = PersistentStorage();

  // 多张图片大图显示
  List imgUrlList = [
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image1.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image2.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image3.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image4.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image5.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image6.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image7.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image8.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image9.jpg',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/coming_soon_bg/image10.jpg'
  ];

  @override
  Future<void> onInit() async {
    bool? hasStorage = await ps.containsKey('userInfo');
    if (hasStorage == true) {
      getNickName();
    } else {
      ps.setStorage(
        'userInfo',
        {
          'name': '江南第一深情',
          'age': '24',
          'job': 'worker',
          'headImg':
              'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/people/dog.jpg',
        },
      );
    }

    super.onInit();
    scrollListener();
  }

  void scrollListener() {
    scrollController.addListener(() {
      if (scrollController.offset < 10) {
        opacity = 0.0;
      }
      if (scrollController.offset >= 10 && scrollController.offset < 13) {
        opacity = 0.1;
      } else if (scrollController.offset >= 13 &&
          scrollController.offset < 16) {
        opacity = 0.1;
      } else if (scrollController.offset >= 16 &&
          scrollController.offset < 19) {
        opacity = 0.3;
      } else if (scrollController.offset >= 19 &&
          scrollController.offset < 22) {
        opacity = 0.4;
      } else if (scrollController.offset >= 22 &&
          scrollController.offset < 25) {
        opacity = 0.5;
      } else if (scrollController.offset >= 25 &&
          scrollController.offset < 28) {
        opacity = 0.6;
      } else if (scrollController.offset >= 28 &&
          scrollController.offset < 31) {
        opacity = 0.7;
      } else if (scrollController.offset >= 31 &&
          scrollController.offset < 34) {
        opacity = 0.8;
      } else if (scrollController.offset >= 34 &&
          scrollController.offset < 36) {
        opacity = 0.9;
      } else if (scrollController.offset >= 36) {
        opacity = 1.0;
      }
      update();
    });
  }

  Future<void> getNickName() async {
    print(await ps.getKeys());
    await ps.getStorage('userInfo').then((value) {
      userName.value = value['name'];
      headImg.value = value['headImg'];
    });
    update();
  }

  Future<void> chooseheadImg(context) async {
    final List<AssetEntity>? result = await AssetPicker.pickAssets(
      context,
      pickerConfig: AssetPickerConfig(
        requestType: RequestType.image,
        gridCount: 3,
        pageSize: 90,
      ),
    );
    if (result == null) return;

    List<String> chooseImagesPath = [];
    List imageList = []; //遍历
    for (var entity in result) {
      File? imgFile = await entity.file;
      if (imgFile != null) {
        chooseImagesPath.add(imgFile.path);
        imageList.add(imgFile);
      }
    }
    chooseImagesPath.forEach((element) {
      print('选择照片路径:$element');
    });
    SmartDialog.show(builder: (context) {
      return SingleChildScrollView(
        child: Column(
          children: [
            Image.file(imageList[0]),
            Image.file(imageList[1]),
            Image.file(imageList[2]),
            Image.file(imageList[3]),
            BackButton(onPressed: () => SmartDialog.dismiss()),
          ],
        ),
      );
    });
  }

  onTapPickFromCamera(context) async {
    final AssetEntity? entity = await CameraPicker.pickFromCamera(context);
    if (entity == null) return;
    File? imgFile = await entity.file;
    if (imgFile == null) return;
    print('照片路径:${imgFile.path}');
  }
}
